#ifndef THINK_THINK_DB_H
#define THINK_THINK_DB_H

static char* think_db =
	"\n"
	"// +----------------------------------------------------------------------\n"
	"// | ThinkPHP [ WE CAN DO IT JUST THINK ]\n"
	"// +----------------------------------------------------------------------\n"
	"// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.\n"
	"// +----------------------------------------------------------------------\n"
	"// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )\n"
	"// +----------------------------------------------------------------------\n"
	"// | Author: liu21st <liu21st@gmail.com>\n"
	"// +----------------------------------------------------------------------\n"
	"\n"
	"namespace think;\n"
	"\n"
	"use think\\db\\Connection;\n"
	"use think\\db\\Query;\n"
	"\n"
	"/**\n"
	" * Class Db\n"
	" * @package think\n"
	" * @method Query table(string $table) static 指定数据表（含前缀）\n"
	" * @method Query name(string $name) static 指定数据表（不含前缀）\n"
	" * @method Query where(mixed $field, string $op = null, mixed $condition = null) static 查询条件\n"
	" * @method Query join(mixed $join, mixed $condition = null, string $type = \'INNER\') static JOIN查询\n"
	" * @method Query union(mixed $union, boolean $all = false) static UNION查询\n"
	" * @method Query limit(mixed $offset, integer $length = null) static 查询LIMIT\n"
	" * @method Query order(mixed $field, string $order = null) static 查询ORDER\n"
	" * @method Query cache(mixed $key = null , integer $expire = null) static 设置查询缓存\n"
	" * @method mixed value(string $field) static 获取某个字段的值\n"
	" * @method array column(string $field, string $key = \'\') static 获取某个列的值\n"
	" * @method Query view(mixed $join, mixed $field = null, mixed $on = null, string $type = \'INNER\') static 视图查询\n"
	" * @method mixed find(mixed $data = null) static 查询单个记录\n"
	" * @method mixed select(mixed $data = null) static 查询多个记录\n"
	" * @method integer insert(array $data, boolean $replace = false, boolean $getLastInsID = false, string $sequence = null) static 插入一条记录\n"
	" * @method integer insertGetId(array $data, boolean $replace = false, string $sequence = null) static 插入一条记录并返回自增ID\n"
	" * @method integer insertAll(array $dataSet) static 插入多条记录\n"
	" * @method integer update(array $data) static 更新记录\n"
	" * @method integer delete(mixed $data = null) static 删除记录\n"
	" * @method boolean chunk(integer $count, callable $callback, string $column = null) static 分块获取数据\n"
	" * @method mixed query(string $sql, array $bind = [], boolean $master = false, bool $pdo = false) static SQL查询\n"
	" * @method integer execute(string $sql, array $bind = [], boolean $fetch = false, boolean $getLastInsID = false, string $sequence = null) static SQL执行\n"
	" * @method Paginator paginate(integer $listRows = 15, mixed $simple = null, array $config = []) static 分页查询\n"
	" * @method mixed transaction(callable $callback) static 执行数据库事务\n"
	" * @method void startTrans() static 启动事务\n"
	" * @method void commit() static 用于非自动提交状态下面的查询提交\n"
	" * @method void rollback() static 事务回滚\n"
	" * @method boolean batchQuery(array $sqlArray) static 批处理执行SQL语句\n"
	" * @method string quote(string $str) static SQL指令安全过滤\n"
	" * @method string getLastInsID($sequence = null) static 获取最近插入的ID\n"
	" */\n"
	"class Db\n"
	"{\n"
	"    /**\n"
	"     * @var Connection[] 数据库连接实例\n"
	"     */\n"
	"    private static $instance = [];\n"
	"\n"
	"    /**\n"
	"     * @var int 查询次数\n"
	"     */\n"
	"    public static $queryTimes = 0;\n"
	"\n"
	"    /**\n"
	"     * @var int 执行次数\n"
	"     */\n"
	"    public static $executeTimes = 0;\n"
	"\n"
	"    /**\n"
	"     * 数据库初始化，并取得数据库类实例\n"
	"     * @access public\n"
	"     * @param  mixed       $config 连接配置\n"
	"     * @param  bool|string $name   连接标识 true 强制重新连接\n"
	"     * @return Connection\n"
	"     * @throws Exception\n"
	"     */\n"
	"    public static function connect($config = [], $name = false)\n"
	"    {\n"
	"        if (false === $name) {\n"
	"            $name = md5(serialize($config));\n"
	"        }\n"
	"\n"
	"        if (true === $name || !isset(self::$instance[$name])) {\n"
	"            // 解析连接参数 支持数组和字符串\n"
	"            $options = self::parseConfig($config);\n"
	"\n"
	"            if (empty($options[\'type\'])) {\n"
	"                throw new \\InvalidArgumentException(\'Undefined db type\');\n"
	"            }\n"
	"\n"
	"            $class = false !== strpos($options[\'type\'], \'\\\\\') ?\n"
	"            $options[\'type\'] :\n"
	"            \'\\\\think\\\\db\\\\connector\\\\\' . ucwords($options[\'type\']);\n"
	"\n"
	"            // 记录初始化信息\n"
	"            if (App::$debug) {\n"
	"                Log::record(\'[ DB ] INIT \' . $options[\'type\'], \'info\');\n"
	"            }\n"
	"\n"
	"            if (true === $name) {\n"
	"                $name = md5(serialize($config));\n"
	"            }\n"
	"\n"
	"            self::$instance[$name] = new $class($options);\n"
	"        }\n"
	"\n"
	"        return self::$instance[$name];\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 清除连接实例\n"
	"     * @access public\n"
	"     * @return void\n"
	"     */\n"
	"    public static function clear()\n"
	"    {\n"
	"        self::$instance = [];\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 数据库连接参数解析\n"
	"     * @access private\n"
	"     * @param  mixed $config 连接参数\n"
	"     * @return array\n"
	"     */\n"
	"    private static function parseConfig($config)\n"
	"    {\n"
	"        if (empty($config)) {\n"
	"            $config = Config::get(\'database\');\n"
	"        } elseif (is_string($config) && false === strpos($config, \'/\')) {\n"
	"            $config = Config::get($config); // 支持读取配置参数\n"
	"        }\n"
	"\n"
	"        return is_string($config) ? self::parseDsn($config) : $config;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * DSN 解析\n"
	"     * 格式： mysql://username:passwd@localhost:3306/DbName?param1=val1&param2=val2#utf8\n"
	"     * @access private\n"
	"     * @param  string $dsnStr 数据库 DSN 字符串解析\n"
	"     * @return array\n"
	"     */\n"
	"    private static function parseDsn($dsnStr)\n"
	"    {\n"
	"        $info = parse_url($dsnStr);\n"
	"\n"
	"        if (!$info) {\n"
	"            return [];\n"
	"        }\n"
	"\n"
	"        $dsn = [\n"
	"            \'type\'     => $info[\'scheme\'],\n"
	"            \'username\' => isset($info[\'user\']) ? $info[\'user\'] : \'\',\n"
	"            \'password\' => isset($info[\'pass\']) ? $info[\'pass\'] : \'\',\n"
	"            \'hostname\' => isset($info[\'host\']) ? $info[\'host\'] : \'\',\n"
	"            \'hostport\' => isset($info[\'port\']) ? $info[\'port\'] : \'\',\n"
	"            \'database\' => !empty($info[\'path\']) ? ltrim($info[\'path\'], \'/\') : \'\',\n"
	"            \'charset\'  => isset($info[\'fragment\']) ? $info[\'fragment\'] : \'utf8\',\n"
	"        ];\n"
	"\n"
	"        if (isset($info[\'query\'])) {\n"
	"            parse_str($info[\'query\'], $dsn[\'params\']);\n"
	"        } else {\n"
	"            $dsn[\'params\'] = [];\n"
	"        }\n"
	"\n"
	"        return $dsn;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 调用驱动类的方法\n"
	"     * @access public\n"
	"     * @param  string $method 方法名\n"
	"     * @param  array  $params 参数\n"
	"     * @return mixed\n"
	"     */\n"
	"    public static function __callStatic($method, $params)\n"
	"    {\n"
	"        return call_user_func_array([self::connect(), $method], $params);\n"
	"    }\n"
	"}\n"
	"\n"
;
#endif